﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Pheonix.CodeLibrary.Web;
using Pheonix.Fashionzoomer.DAL;
using Pheonix.Fashionzoomer.Entity.SqlServer2000;
using Pheonix.CodeLibrary.Data;
using Pheonix.CodeLibrary;
using Pheonix.Fashionzoomer.BLL;

public partial class page_private_admin_productlist : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Title = SiteName + " - Product List [ Admin ]";
        deleteAllButton.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete?');");
    }

    protected void productList_RowCommand(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "editCommand")
        {
            Response.Redirect("productdetail.aspx?id=" + e.CommandArgument.ToString() + "&mode=" +
                FormViewMode.Edit.ToString());
        }
        else if (e.CommandName == "deleteCommand")
        {
            try
            {
                int rowIndex = Convert.ToInt32(e.CommandArgument);
                int productId = (int)productList.DataKeys[rowIndex].Value;

                List<int> idToDelete = new List<int>();
                idToDelete.Add(productId);
                ProductManager.DeleteProductImages(idToDelete);

                Product product = Product.GetProductById(productId);
                CustomCommand.DecreaseCategoryItemCount(product.CategoryId, product.ShopId);

                productList.DeleteRow(Convert.ToInt32(e.CommandArgument)); //command argument contains row index
                uccResponseMessage.SetSuccessMessage("Deleted 1 items");
                
            }
            catch
            {
                uccResponseMessage.SetFailMessage("Failed to delete items");
            }
        }
    }

    protected void productList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            CheckBox selectAll = (CheckBox)e.Row.FindControl("selectAll");
            selectAll.Attributes.Add("onclick", "SelectAll('" + productList.ClientID + "','" + selectAll.ClientID + "')");
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Int32 id = (Int32)productList.DataKeys[e.Row.RowIndex].Value;

            ImageButton editButton = (ImageButton)e.Row.FindControl("editButton");
            editButton.AlternateText = "edit";
            editButton.CommandArgument = id.ToString();

            ImageButton deleteButton = (ImageButton)e.Row.FindControl("deleteButton");
            deleteButton.AlternateText = "delete";
            deleteButton.CommandArgument = e.Row.RowIndex.ToString();
            deleteButton.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete?');");
        }
    }

    protected void addNewButton_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("productdetail.aspx?mode=Insert");
    }

    protected void deleteAllButton_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            GridViewRowCollection productListRows = productList.Rows;
            List<int> idToDelete = new List<int>();

            foreach (GridViewRow row in productListRows)
            {
                CheckBox rowSelection = (CheckBox)row.FindControl("rowSelect");

                if (rowSelection.Checked)
                {
                    idToDelete.Add((int)productList.DataKeys[row.RowIndex].Value);
                }
            }

            if (ProductManager.DeleteProductImages(idToDelete) && Product.DeleteProductList(idToDelete))
            {
                productList.DataBind();
                uccResponseMessage.SetSuccessMessage("Deleted " + idToDelete.Count.ToString() + " items");
            }
            else
            {
                uccResponseMessage.SetFailMessage("Failed to delete items");
            }
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to delete items");
        }
    }
}
